﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace CC.Common
{
    public class XmlHelper
    {
        /// <summary>
        /// 构造函数私有化，防止实例化调用
        /// </summary>
        private XmlHelper() { }
        /// <summary>
        /// 在元素element下加入子元素
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="element"></param>
        /// <param name="name"></param>
        /// <param name="value">为null时创建空节点</param>
        /// <returns>返回新增的子元素</returns>
        public static XmlElement CreateChild(XmlDocument doc, XmlElement element, string name, object value = null)
        {
            XmlElement el = doc.CreateElement(name);
            if (value != null) el.InnerText = value.ToString();
            element.AppendChild(el);
            return (XmlElement)el;
        }
        /// <summary>
        /// 在元素element下加入带属性的子元素
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="element"></param>
        /// <param name="name">子元素名称</param>
        /// <param name="value">要写入的参数值，可以为null</param>
        /// <param name="attributes">可变长度属性，为KeyValue对</param>
        /// <returns></returns>
        public static XmlElement CreateChild(XmlDocument doc, XmlElement element, string name, object value, params KeyValuePair<string, string>[] attributes)
        {
            XmlElement el = doc.CreateElement(name);
            if (value != null) el.InnerText = value.ToString();
            foreach (var a in attributes)
            {
                el.SetAttribute(a.Key, a.Value);
            }
            element.AppendChild(el);
            return el;
        }

        public static object GetSheets(string fileName)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 在元素element下加入带属性的子元素
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="element"></param>
        /// <param name="name">子元素名称</param>
        /// <param name="value">要写入的参数值，可以为null</param>
        /// <param name="attributes">可变长度属性，为string对</param>
        /// <returns></returns>
        public static XmlElement CreateChildWithAttributes(XmlDocument doc, XmlElement element, string name, object value, params string[] attributes)
        {
            XmlElement el = doc.CreateElement(name);
            if (value != null) el.InnerText = value.ToString();
            if(attributes.Length>1)
            {
                for (int i = 0; i < attributes.Length/2; i++)
                {
                    el.SetAttribute(attributes[2*i], attributes[2*i+1]);
                }
            }
            
            element.AppendChild(el);
            return el;
        }
        /// <summary>
        /// 在节点element下添加属性为xsi:nil="true"的空子节点
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="element"></param>
        /// <param name="nodeName"></param>
        /// <returns></returns>
        public static XmlElement CreateNodeWithNullAttr(XmlDocument doc, XmlElement element, string nodeName)
        {
            XmlElement el = doc.CreateElement(nodeName);
            XmlAttribute attr = doc.CreateAttribute("xsi", "nil", "http://www.w3.org/2001/XMLSchema-instance");
            attr.Value = "true";
            el.SetAttributeNode(attr);
            element.AppendChild(el);
            return el;
        }
    }
}
